package com.mfl.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mfl.common.PageResult;
import com.mfl.constant.MessageConstant;
import com.mfl.context.BaseContext;
import com.mfl.exception.CourseAlreadyExistsException;
import com.mfl.mapper.CollectMapper;
import com.mfl.mapper.CourseCategoryMapper;
import com.mfl.mapper.KYCourseVideoMapper;
import com.mfl.pojo.dto.KYCourseVideoDTO;
import com.mfl.pojo.dto.KYCourseVideoPageQueryDTO;
import com.mfl.pojo.entity.Collect;
import com.mfl.pojo.entity.CourseCategory;
import com.mfl.pojo.entity.KYCourseVideo;
import com.mfl.pojo.vo.CourseCategoryVO;
import com.mfl.pojo.vo.KYCourseVideoPageQueryVO;
import com.mfl.pojo.vo.KYCourseVideoVO;
import com.mfl.service.KYCourseVideoService;
import net.sf.jsqlparser.parser.feature.FeatureSet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

@Service
public class KYCourseVideoServiceImpl implements KYCourseVideoService {

    @Autowired
    private KYCourseVideoMapper kyCourseVideoMapper;
    @Autowired
    private CourseCategoryMapper courseCategoryMapper;
    @Autowired
    private CollectMapper collectMapper;

    /**
     * 添加课程视频
     *
     * @param kyCourseVideoDTO
     */
    @Override
    public void insert(KYCourseVideoDTO kyCourseVideoDTO) {
        // 判断课程视频名称是否存在
        KYCourseVideo kyCourseVideo = kyCourseVideoMapper.getBySourceName(kyCourseVideoDTO.getCourseName());
        if (kyCourseVideo != null) {
            throw new CourseAlreadyExistsException(MessageConstant.COURSE_ALREADY_EXISTS);
        }
        KYCourseVideo build = KYCourseVideo.builder()
                .courseName(kyCourseVideoDTO.getCourseName())
                .categoryId(kyCourseVideoDTO.getCategoryId())
                .cover(kyCourseVideoDTO.getCover())
                .video(kyCourseVideoDTO.getVideo())
                .createUser(BaseContext.getCurrentId())
                .createTime(LocalDateTime.now())
                .build();
        kyCourseVideoMapper.insert(build);
    }

    /**
     * 分页查询课程视频
     *
     * @param kyCourseVideoPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(KYCourseVideoPageQueryDTO kyCourseVideoPageQueryDTO) {
        PageHelper.startPage(kyCourseVideoPageQueryDTO.getPage(), kyCourseVideoPageQueryDTO.getPageSize());
        Page<KYCourseVideoPageQueryVO> page = kyCourseVideoMapper.queryByPage(kyCourseVideoPageQueryDTO);
        page.getResult().forEach(item -> {
            // 把课程分类id转换成课程分类名称
            CourseCategory coursecategory = courseCategoryMapper.getByCourseCategoryId(item.getCategoryId());
            item.setCourseCategory(coursecategory.getCourseCategoryName());
        });
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 根据id查询课程视频
     *
     * @param id
     * @return
     */
    @Override
    public KYCourseVideoVO getCourseVideoById(int id) {
        KYCourseVideo kyCourseVideo = kyCourseVideoMapper.getCourseVideoById(id);
        // 根据课程类型id查询类型
        CourseCategory courseCategory = courseCategoryMapper.getByCourseCategoryId(kyCourseVideo.getCategoryId());
        // 查询收藏状态
        Collect collect = collectMapper.selectByUserIdAndBookId(BaseContext.getCurrentId(), id, "video");
        // 统计收藏次数
        int collectNum = collectMapper.selectCount(id, "video");
        return KYCourseVideoVO.builder()
                .id(kyCourseVideo.getId())
                .courseName(kyCourseVideo.getCourseName())
                .courseCategory(courseCategory.getCourseCategoryName())
                .categoryId(kyCourseVideo.getCategoryId())
                .cover(kyCourseVideo.getCover())
                .video(kyCourseVideo.getVideo())
                .isCollect(collect != null ? 1 : 0)
                .collectNum(collectNum)
                .clickCount(kyCourseVideo.getClickCount())
                .createTime(kyCourseVideo.getCreateTime())
                .build();
    }

    /**
     * 修改课程视频
     *
     * @param kyCourseVideoDTO
     */
    @Override
    public void update(KYCourseVideoDTO kyCourseVideoDTO) {
        KYCourseVideo kyCourseVideo = KYCourseVideo.builder()
                .id(kyCourseVideoDTO.getId())
                .courseName(kyCourseVideoDTO.getCourseName())
                .categoryId(kyCourseVideoDTO.getCategoryId())
                .cover(kyCourseVideoDTO.getCover())
                .video(kyCourseVideoDTO.getVideo())
                .updateTime(LocalDateTime.now())
                .updateUser(BaseContext.getCurrentId())
                .build();
        kyCourseVideoMapper.update(kyCourseVideo);
    }

    /**
     * 批量删除课程视频
     *
     * @param ids
     */
    @Override
    public void batchDelete(List<Integer> ids) {
        kyCourseVideoMapper.deleteByIds(ids);
    }

    /**
     * 修改点击量
     * @param id
     */
    @Override
    public void updateClickCount(int id) {
        KYCourseVideo kyCourseVideo = kyCourseVideoMapper.getCourseVideoById(id);
        KYCourseVideo build = KYCourseVideo.builder()
                .id(id)
                .clickCount(kyCourseVideo.getClickCount() + 1)
                .updateTime(LocalDateTime.now())
                .build();
        kyCourseVideoMapper.update(build);
    }
}
